<template>
	<!-- 带返回箭头、标题居中、定位 -->
	<view>
		<view class="header-fixed" :style="{'background':bg_color}">
			<!-- 状态栏 -->
			<view :style="{ 'height': statusBarHeight + 'px'}" class="stat"></view>

			<!-- 标题栏 -->
			<view :style="{ 'height': navigationBarHeight + 'px',}" class="navigation">

				<view v-if="iconShow" class="back">
					<view @click="onback" class="t-icon iconfont icon-zuojiantou" :style="{'color': color}">
					</view>
				</view>
				
				<view class="title" :style="{'color': color}">
					{{title}}
				</view>

			</view>
			<!-- 空白占位,防止定位导致坍塌 -->
		</view>
		<view :style="{ 'height': statusBarHeight + navigationBarHeight + 'px'}"></view>
	</view>
</template>

<script>
	export default {
		props: {
			title: {
				type: String
			},
			color: {
				type: String,
				default: '#333'
			},
			bg_color: {
				type: String,
				default: '#fff'
			},
			iconShow: {
				type: Boolean,
				default: true
			}
		},
		data() {
			return {
				statusBarHeight: 0,
				navigationBarHeight: 0,
			}
		},
		mounted() {
			this.statusBarHeight = getApp().globalData.statusBarHeight
			this.navigationBarHeight = getApp().globalData.navigationBarHeight


		},
		methods: {
			onback() {
				const pages = getCurrentPages()

				// 有可返回的页面则直接返回，uni.navigateBack  默认返回失败之后会自动刷新页面 ，无法继续返回
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}

				// 如果用户刷新网页导致页面返回失败,退回首页
				uni.switchTab({
					url: '/pages/index/index'
				})

				return;
			},
		}
	}
</script>
<style lang="scss">
	.header-fixed {
		position: fixed;
		z-index: 999;
		width: 100%;
		top: 0;
		left: 0;
	}

	.stat {
		height: var(--status-bar-height); // --status-bar-height系统状态栏高度
		width: 100%;
	}

	.navigation {
		color: #fff;
		width: 100%;
		display: flex;
		align-items: center;
		position: relative;

		.back {

			height: 100%;
			position: absolute;
			left: 0;

			.t-icon {
				display: flex;
				align-items: center;
				height: 100%;
				padding: 0 20rpx;
				font-size: 40rpx;
				font-weight: bold;
			}
		}

		.title {
			flex-grow: 1;
			font-size: 36rpx;
			width: 100%;
			text-align: center;
			letter-spacing: 4rpx;
		}
	}
</style>